package com.itheima.controller;

import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(("/clazzs"))
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    /**
     * 查询所有班级信息
     *
     * @return
     */
    @GetMapping("/list")
    public Result findAll() {
        List<Clazz> list = clazzService.findAll();
        return Result.success(list);
    }

    /**
     * 条件分页查询
     *
     * @param clazzQueryParam
     * @return
     */
    @GetMapping
    public Result page(ClazzQueryParam clazzQueryParam) {
        PageResult<Clazz> pageResult = clazzService.page(clazzQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询班级信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Clazz clazz = clazzService.getById(id);
        return Result.success(clazz);
    }

    /**
     * 添加班级信息
     *
     * @param clazz
     * @return
     */
    @PostMapping
    public Result save(@RequestBody Clazz clazz) {
        clazzService.save(clazz);
        return Result.success();
    }

    /**
     * 修改班级信息
     *
     * @param clazz
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Clazz clazz) {
        clazzService.update(clazz);
        return Result.success();
    }


    /**
     * 删除指定ID的班级信息
     *
     * @param id 班级ID，通过路径变量传入
     * @return 删除结果，成功时返回Result.success()
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        // 执行删除操作
        clazzService.delete(id);
        return Result.success();
    }
}
